
cWindow = cWindow or {}
PATH = cWindow.LUA_PATH or ""

dofile(PATH.."basics.lua")
dofile(PATH.."ui.lua")
dofile(PATH.."texcoordmode.lua")
dofile(PATH.."freetexmode.lua")
dofile(PATH.."extracttrismode.lua")
dofile(PATH.."cylindermode.lua")
dofile(PATH.."cylinderfourmode.lua")
dofile(PATH.."spheremode.lua")
dofile(PATH.."cubemode.lua")
dofile(PATH.."planemode.lua")
dofile(PATH.."grassmode.lua")
dofile(PATH.."selectmode.lua")
dofile(PATH.."movescalemode.lua")
dofile(PATH.."rotatemode.lua")
dofile(PATH.."objstatemode.lua")
dofile(PATH.."editmode.lua")
dofile(PATH.."lightmode.lua")
dofile(PATH.."saveload.lua")
dofile(PATH.."myui.lua")

function InitUI()

	UILayout.CreatePop(EditPop)
	EditPop.Hide()

	UILayout.Create(TexWin)
	TexWin.__SelfUI:NoCloseButton()
	TexWin.TexStore = {}
	TexWin.Apply:SetOnClick(function ()
		if SelectMode.HasObj() then
			local Sel = TexWin.ComboTex:GetSelIndex()
			if Sel then
				local TexName = TexWin.ComboTex:GetItem(Sel):GetDXUI(0):GetText()
				for _,o in ipairs(SelectMode.PickObjs) do
					o:SetTexture(TexName)
					o:SetAmbient(0,0,0,0)
				end
			end
		end
	end)
	TexWin.View:SetOnClick(function ()
		local Sel = TexWin.ComboTex:GetSelIndex()
		if Sel then
			local TexName = TexWin.ComboTex:GetItem(Sel):GetDXUI(0):GetText()
			local Tex = g_MgrTex:Find(TexName)
			local ViewWin = cDXUIWindow:New()
			cWindow:AddDXUI(ViewWin)
			ViewWin:SetColor(1,1,1)
			ViewWin:SetAlpha(1)
			ViewWin:SetTitle("ViewTexture")
			ViewWin:SetPos(300,100)
			ViewWin:SetSize(512,512)
			ViewWin:SetTexture(Tex)
		end
	end)
	TexWin.Save:SetOnClick(function ()
		local Sel = TexWin.ComboTex:GetSelIndex()
		if Sel then
			local TexName = TexWin.ComboTex:GetItem(Sel):GetDXUI(0):GetText()
			local Tex = g_MgrTex:Find(TexName)
			if not Tex then
				TRACE("TexWin - Save Invalid tex!\n")
				return
			end
			local Save = cWindow:GetSaveFileName("PNG File|*.png||","png")
			if Save then
				local fullname = Save.Path..Save.FileName
				TRACE(string.format("TexWin - Saving %s to %s\n",TexName,fullname))
				Tex:SaveToPNG(fullname)
			end
		end
	end)
	TexWin.Load:SetOnClick(function ()
		local Ret = cWindow:GetOpenFileNames("Textures|*.*||")
		if Ret then
			for _,v in ipairs(Ret) do
				LoadTexture(Ret.Path,v)
			end
		end
	end)

	UILayout.Create(ShaderWin)
	ShaderWin.__SelfUI:NoCloseButton()
	ShaderWin.__SelfUI:SetPos(0,TexWin.__SelfUI:GetHeight()+30)
	ShaderWin.Load:SetOnClick(function()
		local Ret = cWindow:GetOpenFileNames("FX Shader|*.fx||")
		if Ret then
			for _,v in ipairs(Ret) do
				LoadShader(Ret.Path,v)
			end
		end
	end)
	ShaderWin.Apply:SetOnClick(function()
		if SelectMode.HasObj() then
			local Sel = ShaderWin.ComboShader:GetSelIndex()
			if Sel then
				local ShaderName = ShaderWin.ComboShader:GetItem(Sel):GetText()
				for _,o in ipairs(SelectMode.PickObjs) do
					o:SetShader(ShaderName)
				end
			end
		end
	end)

	UILayout.Create(MainWin)
	MainWin.__SelfUI:SetPos(0,ShaderWin.__SelfUI:GetY()+ShaderWin.__SelfUI:GetHeight()+30)

	UILayout.Create(EditWin)
	EditWin.__SelfUI:NoCloseButton()
	EditWin.EditVertex:SetOnClick(EditVertexFunc)
	EditWin.EditFace:SetOnClick(EditFaceFunc)
	EditWin.__SelfUI:SetPos(300,0)
	local biasx = EditWin.__SelfUI:GetX()+EditWin.__SelfUI:GetWidth()+1

	UILayout.Create(ObjState)
	ObjState.__SelfUI:NoCloseButton()
	ObjState.__SelfUI:SetPos(biasx, 0)
	local ObjStateWinWidth = ObjState.__SelfUI:GetWidth()+1

	g_Camera.Speed = 0.5
	UILayout.Create(CameraState)
	CameraState.__SelfUI:NoCloseButton()
	CameraState.__SelfUI:SetPos(0,MainWin.__SelfUI:GetY()+MainWin.__SelfUI:GetHeight()+10)
	CameraState.Speed:SetText("0.5")
	CameraState.Set:SetOnClick(function ()
		local Speed = tonumber(CameraState.Speed:GetText())
		if not Speed then
			TRACE("CameraState - Invalid speed!\n")
			return
		end
		g_Camera.Speed = Speed
	end)
	CameraState.X2:SetOnClick(function ()
		local Speed = tonumber(CameraState.Speed:GetText())
		if not Speed then
			TRACE("CameraState - Invalid speed!\n")
			return
		end
		Speed = Speed*2
		CameraState.Speed:SetText(tostring(Speed))
		g_Camera.Speed = Speed
	end)
	CameraState.Half:SetOnClick(function ()
		local Speed = tonumber(CameraState.Speed:GetText())
		if not Speed then
			TRACE("CameraState - Invalid speed!\n")
			return
		end
		CameraState.Speed:SetText(tostring(Speed/2))
		g_Camera.Speed = Speed
	end)

end

cWindow.OnCreate = function ()

	local WinSize = cWindow:GetSize()
	g_Camera = cWindow:GetCamera()
	local aspect = WinSize.x/WinSize.y
	g_Camera:BuildProj(math.pi/2,aspect,0.1,10000.0)
	g_Camera:SetSpeed(100.0)
	g_Camera:MoveStraight(false)

	TRACE("OnCreate - GetMangers Begin\n")
	g_MgrMesh = cWindow:GetManagerMesh()
	g_MgrShader = cWindow:GetManagerShader()
	g_MgrTex = cWindow:GetManagerTexture()
	g_MgrObj = cWindow:GetManagerObj()
	TRACE("OnCreate - GetManagers End\n")

	CreateBasicMeshes()

	TRACE("OnCreate - Modes Begin\n")
	AxisXYZ.Create()
	SelectMode.CreateUnit()
	MarqueeMode.CreateUnit()
	PlaneMode.CreateUnit()
	CubeMode.CreateUnit()
	SphereMode.CreateUnit()
	CylinderMode.CreateUnit()
	CylinderFourMode.CreateUnit()
	GrassMode.CreateUnit()
	RotateMode.CreateUnit()
	TexcoordMode.CreateUnit()
	FreeTexMode.CreateUnit()
	ExtractTrisMode.CreateUnit()
	LightPropMode.CreateUnit()
	LightMode.CreateUnit()
	SpotLightMode.CreateUnit()
	DirLightMode.CreateUnit()
	PointLightMode.CreateUnit()

	AxisControl.CreateUnit()
	MoveMode.CreateUnit()
	ScaleMode.CreateUnit()
	TRACE("OnCreate - Modes End\n")


	--UI
	TRACE("OnCreate - UI Begin\n")
	InitUI()
	TRACE("OnCreate - UI End\n")

	TRACE("OnCreate - Texture Begin\n")
	TRACE("OnCreate - Texture End\n")



	TRACE("OnCreate - Shader Begin\n")
	AddItemToShaderCombo("mesh.fx")
	local shader = cShader:New()
	g_MgrShader:Add("mesh.fx",shader)
	if not shader:CreateFromFile(cWindow:GetDevice(),PATH.."mesh.fx") then
		g_MgrShader:Remove("mesh.fx")
		return false
	end

	AddItemToShaderCombo("ui.fx")
	local ShaderUI = cShader:New()
	g_MgrShader:Add("ui.fx",ShaderUI)
	if not ShaderUI:CreateFromFile(cWindow:GetDevice(),PATH.."ui.fx") then
		g_MgrShader:Remove("ui.fx")
		return false
	end

	AddItemToShaderCombo("shadow.fx")
	local ShaderShadow = cShader:New()
	g_MgrShader:Add("shadow.fx",ShaderShadow)
	if not ShaderShadow:CreateFromFile(cWindow:GetDevice(),PATH.."shadow.fx") then
		g_MgrShader:Remove("shadow.fx")
		return false
	end
	TRACE("OnCreate - Shader End\n")
	return true
end

function CameraControl()
	if cWindow:MouseButtonDown(DIK.MBUTTON) then
		local dx = cWindow:MouseDX()
		local dy = cWindow:MouseDY()
		if dx~=0 then g_Camera:RotateWorldY(dx/100) end
		if dy~=0 then g_Camera:RotateX(dy/100) end
		Ray = cWindow:GetPickRay()
	end
	g_Camera:Confine()
	local Speed = g_Camera.Speed
	if cWindow:KeyDown(DIK.LCONTROL) then Speed = Speed/4 end
	g_Camera:SetSpeed(Speed)
	if cWindow:KeyDown(DIK.W) then g_Camera:MoveStraight(true) end
	if cWindow:KeyDown(DIK.S) then g_Camera:MoveStraight(false) end
	if cWindow:KeyDown(DIK.A) then g_Camera:MoveRight(false) end
	if cWindow:KeyDown(DIK.D) then g_Camera:MoveRight(true) end
	if cWindow:KeyDown(DIK.E) then g_Camera:MoveUp(true) end
	if cWindow:KeyDown(DIK.Q) then g_Camera:MoveUp(false) end

end

g_CurMode = nil
function Main_1()
	CameraControl()
	if g_CurMode then g_CurMode.CurFuncSeq() end
end
function RButtonPopup (mx,my)
	if not EditPop.Visible then
		mx = mx + 2
		my = my + 2
		local winsize = cWindow:GetClientSize()
		local biasx = mx + EditPop.width - winsize.x
		local biasy = my + EditPop.height - winsize.y
		if biasx > 0 then mx = mx - biasx end
		if biasy > 0 then my = my - biasy end
		EditPop.MoveTo(mx,my)
		EditPop.Show()
	else
		EditPop.Hide()
	end

end
cWindow.Main = Main_1
cWindow.OnRButtonDown = RButtonPopup
